/**
 * Table model for Display data
 * Management of the Display Table
 * 
 * @author J.J.G.
 */

package fr.emse.tatiana.display;

import javax.swing.table.AbstractTableModel;


public class DisplayTableModel extends AbstractTableModel
{
	static final long serialVersionUID = 0201;
	
    DisplayTable dt;

    /**
     * This creates a new DisplayTableModel ; 
     * @param theTable : the DisplayTable that is supposed to be
     * managed ; 
     */
    public DisplayTableModel (DisplayTable theTable)
    {
        dt = theTable;
    }

    /**
     * Return the presently "visible" number of columns 
     * in the table ; programs should always use this interface
     * since DisplayTables may be more complex in the future...
     */
    public int getColumnCount() {
        return dt.colnb;
    }

    /**
     * Return the presently "visible" number of rows
     * in the table ; programs should always use this interface
     * since DisplayTables may be more complex in the future...
     */
    public int getRowCount() {
        return dt.rownb;
    }

    /**
     * Returns the value of a cell in the table
     * @param row : (visible) line number of the cell 
     * @param col : (visible) column number of the cell 
     */
     public Object getValueAt(int row, int col) {
    	// System.out.println("    getValueAt["+row+"]["+col+"]");
        return dt.data[row][col];
    }

    /**
     * Returns the name of the column
     * @param col : (visible) column number 
     */
    @Override
	public String getColumnName(int col) {
        return (String) dt.aux[DisplayTable.CNames][col];
    }

    /**
     * Returns the "class" of the column
     * @param col : (visible) column number 
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    @Override
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int col) {
    	/*
    	 * This version looks for the furst non null
    	 * Object in the column... 
    	 *
    	Object ce;
    	int i, rc;
    	rc = getRowCount();
    	for (i=0; i<rc; i++)
    	{
    		ce = getValueAt(i,col);
    		if (ce != null) return ce.getClass();
    	}
        return "".getClass();
        */
    	// We say we want only the "DisplayCellRenderer" for the job...
    	// return DisplayCellRenderer.class;
    	return Object.class;
    }

    /**
     * Returns true is the current cell is editable
     * Could be based on the value of the cell, or on
     * some other characteristics
     * @param row : (visible) line number of the cell 
     * @param col : (visible) column number of the cell 
     */
    @Override
	public boolean isCellEditable(int row, int col) {
         // return (Boolean)dt.aux[DisplayTable.CEdit][col];
         // return true;
         return (Boolean)dt.aux[DisplayTable.CEdit][col];
    }
  
    
    /**
     * Change the value of a cell in the DisplayTable
     * @param value : any Object, even null, to be put in the cell
     * @param row : (visible) line number of the cell 
     * @param col : (visible) column number of the cell 
     */
    @Override
	public void setValueAt(Object value, int row, int col) {
         dt.data[row][col] = value;
         fireTableCellUpdated(row, col);
    }
    
    /**
     * An internal method which, given the number of a "visible" row,
     * returns the internal number of the row
     * @param row : the visible row number
     * @return : the internal row number
     */
    private int getInternalRow(int row)
    {
    	return row;
    }

    /**
     * An internal method which, given the number of a "visible" column,
     * returns the internal number of the row
     * @param col : the visible column number
     * @return : the internal column number
     */
    private int getInternalCol(int col)
    {
    	return col;
    }
}

